package com.tanhua.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.UserInfoApi;
import com.tanhua.db.mapper.BlackListMapper;
import com.tanhua.db.mapper.UserInfoMapper;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.pojo.UserInfo;
import netscape.security.Privilege;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlackListMapper blackListMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public void updateById(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据用户id  分页查询黑名单用户的详情
    @Override
    public Page<UserInfo> findByPage(Long userId, Integer page, Integer pagesize) {
        //1 创建分页对象
        Page<UserInfo> pages = new Page<>(page, pagesize);

        //2 执行分页查询
        pages = blackListMapper.findBlackList(pages, userId);

        //3 返回结果
        return pages;
    }

    //根据 推荐好友id 和 查询条件 分页 查询用户详情 UserInfo
    @Override
    public Page<UserInfo> findByDto(List<Long> ids, RecommendUserDto dto) {
        //1.封装分页对象
        Page<UserInfo> pages =new Page<>(dto.getPage(),dto.getPagesize());
        //2.封装查询条件 条件有可能为空
        LambdaQueryWrapper<UserInfo> warpper = Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo ::getId,ids)//设置推荐好友的ids作为条件
                .eq(StringUtils.isNotBlank(dto.getGender()),UserInfo::getGender,dto.getGender()) //根据性别查询
                .lt(dto.getAge()!=null,UserInfo::getAge,dto.getAge());//查询小于指定的年龄

        //3.执行查询
        pages =userInfoMapper.selectPage(pages,warpper);
        return pages;
    }


    //查询好友动态, 不返回查询总记录数
    @Override
    public List<UserInfo> findByIds(List<Long> ids) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getId, ids));
    }


    //根据好友的ids 昵称的关键词 分页查询好友的用户详情
    @Override
    public List<UserInfo> findByPage(List<Long> ids, String keyword, Integer page, Integer pagesize) {
        //1.封装查询条件
        Page<UserInfo> pages = new Page<>(page,pagesize);

        //2.封装分页对象
        LambdaQueryWrapper<UserInfo> wrapper = Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getId, ids)
                .like(StringUtils.isNotBlank(keyword), UserInfo::getNickname, keyword);

        //3.执行查询
        pages = userInfoMapper.selectPage(pages, wrapper);

        //4.返回结果
        return pages.getRecords();
    }


    //根据ids和性别查询用户详情
    @Override
    public List<UserInfo> findByIds(List<Long> ids, String gender) {

        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
            .in(UserInfo ::getId,ids)
            .eq(StringUtils.isNotBlank(gender),UserInfo::getGender,gender));
    }


    //分页查询用户详情
    @Override
    public Page<UserInfo> findByPage(Integer page, Integer pagesize) {

        //1.封装分页对象
        Page<UserInfo> pages = new Page<>(page,pagesize);

        //2.执行查询
        pages = userInfoMapper.selectPage(pages,null);
        return pages;
    }


}
